<template>
  <div class="container">
    <el-container>
      <el-header>
        <div class="header-left">教你学双语后台系统</div>
        <div><i class="iconfont">&#xe696;</i><span>{{this.$store.state.userinfo.name}}</span>
          <el-button type="text" round @click="logout"><i class="iconfont">&#xe78e;</i> 退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside width="260px">
          <slide-bar :navbarlist="navbarlist"></slide-bar>
        </el-aside>
        <router-view v-if="isRouterAlive" :title="title" :upload="upload" :key="key"/>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import SlideBar from './navbar/navbar'
  import axios from 'axios'
  import {
    modulefather, moduleson
  } from '@/api/navbar/navbar'

  export default {
    name: "Home",
    components: {
      SlideBar
    },
    provide() {
      return {
        reload: this.reload
      }
    },
    data() {
      return {
        navbarlist: [
        ],
        subnavlist: [],
        isRouterAlive: true,
        title: [],
        imgs: '',
        name: '',
        userInfo: [],
        upload:'http://39.105.59.249/account/a/u/uploadfile'
      }
    },
    methods: {
      reload() {
        this.isRouterAlive = false;
        this.$nextTick(function () {
          this.isRouterAlive = true
        })
      },
      logout() {
        localStorage.removeItem('ms_username');
        localStorage.clear();
        window.location.reload();
      },
      getNavbarInfo() {
        let _this = this;
        moduleson().then(function (res) {
          for (let item of res.data.UserList) {
            _this.subnavlist.push({
              index: item.modelUrl,
              id: item.id,
              management: item.modelName
            })
          }
          return JSON.parse(localStorage.userinfo).id
        }).then(this.getsub)
        // axios.get('/api/navbar.json').then(this.getNavbarInfoSucc);
        // axios.get('/reading/getsub').then(this.getsub);
      },
      getsub(res) {
        let params={};
        let _this=this;
        params.id=res;
        modulefather(params).then(function (res) {
          for(let item of res.data.UserList){
            _this.navbarlist.push({
              index:item.modelUrl,
              management:item.modelName,
              menuid:item.menuid,
              icon:item.modelIcon,
              modelPart:item.modelPart
            })
          }
          for(let item of _this.navbarlist){
            switch (item.menuid){
              case 1:
                item.show=true;
                break;
              case 2:
                item.show=false;
                break;
            }
            item.model=item.modelPart.split(',');
            item.subnavlist=[]
          }
          for(let item of _this.navbarlist){
            for(let model of item.model){
              for(let sub of _this.subnavlist){
                if(model==sub.id){
                  item.subnavlist.push(sub)
                }
              }
            }
          }
        })
      },
      getNavbarInfoSucc(res) {
      },
      open() {
        let time = this.$moment(Date.now()).format("YYYY-MM-DD");
        const h = this.$createElement;
        this.$notify({
          title: '欢迎回来，亲爱的' + this.$store.state.userinfo.name,
          message: h('div', {style: 'color: #409EFF'}, '今天是' + time + '号 ' + " 祝您有快乐的一天")
        });
      },
    },
    mounted() {
      this.getNavbarInfo();
      this.open();
    },
    computed: {
      key() {
        return this.$router.name !== undefined ? this.$router.name + new Date() : this.$route + new Date()
      }
    }
  }
</script>

<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";

  .container {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
  }

  .el-container {
    width: 100%;
  }

  .el-header {
    padding: 0 0;
    .flex;
    .header-left {
      color: white;
      text-align: center;
      height: 60px;
      line-height: 60px;
      width: 260px;
      .bgcolor(#00284d);
      font-weight: bold;
      font-size: 22px;
    }
    & div:last-child {
      flex: 1;
      text-align: right;
      padding-right: 50px;
      padding-top: 20px;
      font-size: 22px;
      color: #409EFF;
      span {
        padding: 20px;
        margin-top: 20px;
      }
      .iconfont {
        font-size: 22px !important;
      }
    }
  }
  .el-aside {
    padding-bottom: 50%;
    background-color: #001529;
  }

  .el-main {
  }
</style>
